package com.hzj.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
public class FileController {
    @RequestMapping("/toUpload")
    public String toUpload(){
        return "jsp/upload";
    }
    @RequestMapping("/upload")
    public String fileUpload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
        //指定上传路径
        String path=request.getServletContext().getRealPath("/upload");
        File realPath = new File(path);
        if (!realPath.exists()){
            realPath.mkdir();
        }

        System.out.println("上传文件保存地址"+realPath);

        file.transferTo(new File(realPath+"/"+file.getOriginalFilename()));

        return "redirect:toUploadSuccess";
    }


    @RequestMapping("toUploadSuccess")
    public String toUploadSuccess(){
        return "success/uploadSuccess";
    }


}
